﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace UserControls
{
    public partial class ContactUC : StudioUserControl
    {
        private Int64 _idPartener = -1;
        public Int64 IdPartener
        {
            get { return _idPartener; }
            set
            {
                if (_idPartener != value)
                {
                    _idPartener = value;
                    SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
                    this.contactTableAdapter.Connection = con;
                    if (_idPartener == -1)
                        this.contactTableAdapter.Fill(this.eSRPDataSet.Contact);
                    else
                    {
                        this.contactTableAdapter.FillByIdPartener(this.eSRPDataSet.Contact, _idPartener);
                        eSRPDataSet.Contact.IDPartenerColumn.DefaultValue = _idPartener;
                    }
                    StudioForm.InchideConexiune(con);
                    if (contactBindingSource.Count == 0)
                        contactBindingSource.AddNew();
                }
            }
        }

        public ContactUC()
        {
            InitializeComponent();
            stareCivilaBindingSource.Add(new TipPartenerSursa("Casatorit", "CS"));
            stareCivilaBindingSource.Add(new TipPartenerSursa("Necasatorit", "NC"));
            stareCivilaBindingSource.Add(new TipPartenerSursa("Divortat", "DV"));
            stareCivilaBindingSource.Add(new TipPartenerSursa("Vaduva", "VA"));
            stareCivilaBindingSource.Add(new TipPartenerSursa("Recasatorit", "RC"));


            tipAIBindingSource.Add(new TipPartenerSursa("Carte de Identitate", "CI"));
            tipAIBindingSource.Add(new TipPartenerSursa("Buletin de Identitate", "BI"));
            tipAIBindingSource.Add(new TipPartenerSursa("Pasaport", "PS"));
            tipAIBindingSource.Add(new TipPartenerSursa("Certificat nastere", "CN"));
            tipAIBindingSource.Add(new TipPartenerSursa("Livret militar", "LM"));
            
        }

        public void InitDB(StudioForm sf)
        {
            if (parentForm == null)
            {
                parentForm = sf;
            }
            if (parentForm == null)
                return;
            SqlConnection con = StudioForm.DeschideConexiune(parentForm.workConnString);
            this.tipContactTableAdapter.Connection = con;
            this.tipContactTableAdapter.Fill(this.eSRPDataSet.TipContact);
            StudioForm.InchideConexiune(con);

        }

        private void ContactUC_Load(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            eSRPDataSet.Contact.StareCivilaColumn.DefaultValue = "NC";
            eSRPDataSet.Contact.TipAIColumn.DefaultValue = "CI";
            eSRPDataSet.Contact.IDTipContactColumn.DefaultValue = 5;

            parentForm.listBndSourceMain.Add(contactBindingSource);
            parentForm.listTableAdaptor.Add(contactTableAdapter.Adapter);
        }

        private void ContactUC_Validating(object sender, CancelEventArgs e)
        {
            Boolean eEroare = false;
            if (numeTB.Text.Trim() == String.Empty)
            {
                //MessageBox.Show("Va rugam completati numarul documentului!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
                errorProvider1.SetError(numeTB, "Va rugam completati numele persoanei de contact!");
                eEroare = true;
            }
            else
                errorProvider1.SetError(numeTB, String.Empty);
            /*if (iDTipAdresaCB.SelectedIndex == -1)
            {
                errorProvider1.SetError(iDTipAdresaCB, "Va rugam completati tipul de adresa!");
                eEroare = true;
            }
            else
                errorProvider1.SetError(iDTipAdresaCB, String.Empty);
            */

            if (eEroare)
                return;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;

            if (ContactTC.SelectedTab == PersoanaTP)
                parentForm.SetCurrentDataSource(contactBindingSource);
            else
                if (ContactTC.SelectedTab == InfoContactTP)
                {
                    infoContactUC1.SetCurrentDataSource();

                }
        }

        private void ContactUC_VisibleChanged(object sender, EventArgs e)
        {
            if (parentForm == null)
                return;
            if (Visible)
            {
                parentForm.SetCurrentDataSource(contactBindingSource);
            }
        }

        private void ContactTC_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (parentForm == null)
                return;
            parentForm.SetCurrentDataSource(contactBindingSource);
            if (ContactTC.SelectedTab == InfoContactTP)
            {
                DataRowView dr = ((DataRowView)contactBindingSource.Current);
                if ((dr != null) && dr.IsNew)
                {
                    e.Cancel = true;
                    MessageBox.Show("Va rugam sa finalizati adaugarea persoanei de contact inainte de a trece la celelalte sectiuni ale machetei!\n",
                        "Stop",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Stop);
                    return;
                }

                ESRPDataSet.ContactRow dr2 = ((DataRowView)contactBindingSource.Current).Row as ESRPDataSet.ContactRow;
                infoContactUC1.parentForm = parentForm;
                if (ContactTC.SelectedTab == InfoContactTP)
                {
                    infoContactUC1.IdContact = dr2.IDContact;
                }
            }
        }

        private void contactBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            ESRPDataSet.ContactRow dr2 = ((DataRowView)contactBindingSource.Current).Row as ESRPDataSet.ContactRow;
            infoContactUC1.IdContact = dr2.IDContact;
        }

        private void DeleteError_Enter(object sender, EventArgs e)
        {
            errorProvider1.SetError((Control)sender, String.Empty);
        }
    }
}
